package communication 
{
	import flash.utils.Dictionary;
	/**
	 * 通信管理池
	 * 模块间通信，消息与参数的传递
	 * @author gcxk721
	 */
	public final class CommunicationPool 
	{
		
		/**
		 * 用来记录的容器
		 * key
		 * Vecter.<ICommunication>
		 */
		private static var m_Record : Dictionary = new Dictionary(true);
		
		/**
		 * 特殊记录容器
		 */
		private static var m_SpecialRecord : Dictionary = new Dictionary(true);
		
		public function CommunicationPool() 
		{
			
		}
		
		/**
		 * 注册一条消息监听
		 * @param	key
		 * @param	icom
		 */
		public static function enroll(key : * , icom : ICommunication) : void
		{
			var vec : Vector.<ICommunication>;
			if (m_Record[key] == undefined)
			{
				vec = new Vector.<ICommunication>();
				m_Record[key] = vec;
			}
			else
			{
				vec = m_Record[key];
				if (checkEnroll(vec, icom))
				{
					//已经注册了监听,返回
					return;
				}
			}
			vec.push(icom);
		}
		
		private static function checkEnroll(vec : Vector.<ICommunication>, icom : ICommunication) : Boolean
		{
			for (var i : int = 0; i < vec.length; ++i)
			{
				if (vec[i] == icom)
				{
					return true;
				}
			}
			return false;
		}
		
		/**
		 * 检查消息结构中是否包含了当前的消息
		 * @param	key
		 * @param	icom
		 * @return
		 */
		public static function contains(key : * , icom : ICommunication) : Boolean
		{
			if (m_Record[key] == undefined)
			{
				return false;
			}
			var vec : Vector.<ICommunication> = m_Record[key];
			for (var i : int = 0; i < vec.length; ++i)
			{
				if (vec[i] == icom)
				{
					return true;
				}
			}
			return false;
		}
		
		/**
		 * 注销一条通信监听
		 * @param	key
		 * @param	icom
		 */
		public static function logout(key : * , icom : ICommunication) : void
		{
			var vec : Vector.<ICommunication> = m_Record[key];
			if (vec == null)
			{
				return;
			}
			for (var i : int = 0; i < vec.length; ++i)
			{
				if (vec[i] == icom)
				{
					vec.splice(i, 1);
					return;
				}
			}
		}
		
		/**
		 * 发送一条通信消息
		 * @param	key
		 * @param	data
		 */
		public static function send(key : * , data : * = null) : void
		{
			var vec : Vector.<ICommunication> = m_Record[key];
			if (vec == null)
			{
				return;
			}
			for (var i : int = 0; i < vec.length; ++i)
			{
				vec[i].onCommnuication(key, data);1
			}
		}
		
	}

}